var express = require('express');
var router = express.Router();
var mg = require("./mongo")
var redis = require("./redis")
var pu = require("./public")
var uuid = require("uuid")

var play = require("./play")
/* GET users listing. */
/**
 * 1、管理登录后台
 * 2、开通合作商
 */

var adminloginToken = ""
router.post('/adminlogin', function (req, res, next) {
    if (req.body && req.body.pwd && req.body.pwd == "33897624X") {//
        adminloginToken = uuid.v4()
        pu.successadmin("1", adminloginToken, res)
    } else {
        pu.error("异常操作", 502, res)
    }
});
//代理升级
router.post('/vipadd', async function (req, res, next) {
    let userid = req.body.id
    let token = req.body.token
    if (await redis.get(userid) !== token) {
        pu.failadmin("登录过期", res)
        return
    }

    let userInfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(userid)})
    if (userInfo.dianshu < 99) {
        pu.failadmin("点数不足", res)
        return
    } else {

        incdianshu(userid, -99, "代理升级")
        mg.updateOne("hezuoshang", {_id: mg.ObjectID(userid)}, {$inc: {dianshu: -99}, $set: {lv: 2, alltestKey: 10}})
        pu.successadmin("1", userInfo.dianshu - 99, res)
    }

})
//代理升级
router.post('/findDili', async function (req, res, next) {
    let userid = req.body.id
    let token = req.body.token
    if (await redis.get(userid) !== token) {
        pu.failadmin("登录过期", res)
        return
    }

    let userInfo = await mg.find("hezuoshang", {lastUserid:userid})
    pu.successadmin("1", userInfo, res)


})


router.post('/findDianshu', async function (req, res, next) {
    let userid = req.body.id
    let token = req.body.token
    if (await redis.get(userid) !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let userInfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(userid)})
    if (!userInfo) {
        pu.failadmin("ID不正确", res)
        return
    }
    pu.successadmin("1", userInfo.dianshu, res)

})
router.post('/hezuoshang', async function (req, res, next) {
    /**
     * 1、检测账号是否重复
     * 2、检测余额是否充足 上家设置需要金额
     * 3、扣除开通需要点数 递归向上扣费
     */
    let userid = req.body.id
    let email = req.body.email
    let token = req.body.token
    if (await redis.get(userid) !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    //检测参数
    let userInfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(userid)})
    if (!userInfo) {
        pu.failadmin("ID不正确", res)
        return
    }

    var lastUserinfo
    if (!userInfo.lastUserid) {
        lastUserinfo = {
            houtaimoney: 186
        }
    } else {

        lastUserinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(userInfo.lastUserid)})
    }

    //检测点数
    if (userInfo.dianshu < lastUserinfo.houtaimoney) {
        pu.failadmin("点数不足", res)
        return
    }
    //检测变动
    if (userInfo.houtaimoney < lastUserinfo.houtaimoney) {
        pu.failadmin("上家设置变化 请重新设置后台价格", res)
        return
    }


    //检测邮箱
    let info = await mg.findOne("hezuoshang", {email: email})
    if (!info) {
        let userinfo = {
            email: email,
            pwd: email.split("@")[0],
            dianshu: 0,
            is_delete: 0,
            name: "去延迟秒盘",
            jiejian: "请联系客服" + email,
            testkey: 0,//测试卡总数
            alltestKey: 3,
            lv: 1,
            houtaimoney: userInfo.houtaimoney + 80 || 249,
            day: userInfo.day + 5 || 15, //天卡价格
            math: userInfo.math + 50 || 149, //周卡价格
            threemath: userInfo.threemath + 100 || 349,//季度卡价格
            year: userInfo.year + 200 || 1399,//年卡价格
            lastUserid: userid,
        }
        await  mg.insert("hezuoshang", userinfo)


        await mg.updateOne("hezuoshang", {_id: userInfo._id}, {$inc: {dianshu: -lastUserinfo.houtaimoney}})
        incdianshu(userInfo._id.toString(), -lastUserinfo.houtaimoney, "开通代理后台")

        pu.successadmin("1", userinfo.dianshu - lastUserinfo.houtaimoney, res)
        if (lastUserinfo._id) {
            playdis(lastUserinfo._id.toString(), "houtaimoney",userInfo.email)
        }
    } else {
        pu.failadmin("账号已经存在", res)
        return
    }


});


router.post('/updatePwd', async function (req, res, next) {
    let id = req.body.id
    let token = req.body.token
    let newpwd = req.body.newpwd
    if (await redis.get(id) !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    if (!newpwd) {
        pu.failadmin("密码不能空", res)
        return
    }
    if (newpwd.length < 6) {
        pu.failadmin("密码不能小于6个字符", res)
        return
    }
    mg.updateOne("hezuoshang", {_id: mg.ObjectID(id)}, {$set: {pwd: newpwd}})
    pu.successadmin("成功", "", res)

})

router.post('/updateUserMoney', async function (req, res, next) {

    let _id = req.body.id
    let token = req.body.token
    var info = req.body.info
    if (await redis.get(_id) !== token || _id.length != 24) {
        pu.failadmin("登录过期", res)
        return
    }
    let userinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(_id)})
    var lastUserinfo
    if (!userinfo.lastUserid) {
        lastUserinfo = {
            houtaimoney: 186,
            day: 12,
            math: 99,
            threemath: 249,
            year: 988
        }
    } else {
        lastUserinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(userinfo.lastUserid)})

    }
    var s = 0
    for (var obj in info) {
        s += 1
        if (isNaN(Number(info[obj]))) {
            pu.failadmin("参数格式不正确", res)
            return
        }
        if (Number(info[obj]) < lastUserinfo[obj] && obj!="year") {
            pu.failadmin("参数不能低于上家设置", res)
            return
        }
        info[obj] = Number(Number(info[obj]))
    }
    if (s !=4) {
        pu.failadmin("参数格式不正确", res)
        return
    }
    await mg.update("hezuoshang", {_id: mg.ObjectID(_id)}, {
        $set: {
            houtaimoney: info.houtaimoney,
            day: info.day,
            math: info.math,
            threemath: info.threemath,
            year: info.year
        }
    })
    pu.successadmin("成功", "", res)
})


router.post('/addkeys', async function (req, res, next) {
    let _id = req.body.id
    let token = req.body.token
    let key = req.body.type

    if (await redis.get(_id) !== token) {
        pu.failadmin("登录过期", res)
        return
    }

    let userInfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(_id)})


    var lastUserinfo
    if (!userInfo.lastUserid) {
        lastUserinfo = {
            houtaimoney: 186,
            day: 12,
            math: 99,
            threemath: 249,
            year: 988
        }
    } else {
        lastUserinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(userInfo.lastUserid)})
    }
    //检测点数
    if (userInfo.dianshu < lastUserinfo[key]) {
        pu.failadmin("点数不足", res)
        return
    }
    //检测变动
    if (userInfo[key] < lastUserinfo[key]) {
        pu.failadmin("上家设置变化 请重新设置后台价格", res)
        return
    }

    await mg.updateOne("hezuoshang", {_id: mg.ObjectID(_id)}, {$inc: {dianshu: -lastUserinfo[key]}})

    incdianshu(_id, -lastUserinfo[key], "开卡")


    var starttime = 0
    if (key == "day") {
        starttime = 1000 * 60 * 60 * 24
    }
    if (key == "math") {
        starttime = 1000 * 60 * 60 * 24 * 30
    }

    if (key == "threemath") {
        starttime = 1000 * 60 * 60 * 24 * 90
    }
    if (key == "year") {
        starttime = 1000 * 60 * 60 * 24 * 365
    }
    var keys = uuid.v4()
    let keyInfo = {
        keys: keys,
        stopTime: 0,
        starttime: starttime,
        time: new Date().getTime(),
        uid: userInfo._id
    }
    await mg.insert("keys", keyInfo)
    pu.successadmin("成功", {key: keys, bei: "", dianshu: -lastUserinfo[key]}, res)


    if (lastUserinfo._id) {
        playdis(lastUserinfo._id.toString(), key,userInfo.email)
    }

})
router.post('/getUserinfo', async function (req, res, next) {
    let _id = req.body.id
    let token = req.body.token
    if (!_id) {
        pu.failadmin("登录过期", res)
        return
    }
    if (await redis.get(_id) !== token || _id.length != 24) {
        pu.failadmin("登录过期", res)
        return
    }
    let info = await mg.findOne("hezuoshang", {_id: mg.ObjectID(_id)})
    var lastUserinfo
    if (!info.lastUserid) {
        lastUserinfo = {
            houtaimoney: 186,
            day: 12,
            math: 99,
            threemath: 249,
            year: 988
        }
    } else {

        lastUserinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(info.lastUserid)})

    }

    if (lastUserinfo) {
        info.lastUserinfo = {
            houtaimoney: lastUserinfo.houtaimoney || 186,
            day: lastUserinfo.day || 12,
            math: lastUserinfo.math || 99,
            threemath: lastUserinfo.threemath || 249,
            year: lastUserinfo.year || 988
        }
    } else {
        info.lastUserinfo = {
            houtaimoney: 186,
            day: 12,
            math: 99,
            threemath: 249,
            year: 988
        }
    }
    delete info.pwd
    pu.successadmin("1", info, res)

})

router.post('/hezuologin', async function (req, res, next) {
    let pwd = req.body.pwd
    let email = req.body.email
    let info = await mg.findOne("hezuoshang", {email: email, pwd: pwd})
    if (info) {
        var lastUserinfo = {}
        if (!info.lastUserid) {
            lastUserinfo = {
                houtaimoney: 186,
                day: 12,
                math: 99,
                threemath: 249,
                year: 988
            }
        } else {

            lastUserinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(info.lastUserid)})

        }
        if (info.is_delete != 0) {
            pu.failadmin("账户被禁用", res)
        } else {
            let token = uuid.v4()
            await redis.set(info._id.toString(), token, 1 * 60 * 60 * 24 * 7)
            info.token = token
            if (lastUserinfo) {
                info.lastUserinfo = {
                    houtaimoney: lastUserinfo.houtaimoney || 186,
                    day: lastUserinfo.day || 12,
                    math: lastUserinfo.math || 99,
                    threemath: lastUserinfo.threemath || 249,
                    year: lastUserinfo.year || 988
                }
            } else {
                info.lastUserinfo = {
                    houtaimoney: 186,
                    day: 12,
                    math: 99,
                    threemath: 249,
                    year: 988
                }
            }
            delete info.pwd
            pu.successadmin("1", info, res)
        }

    } else {
        pu.failadmin("密码错误", res)
        return
    }
});


router.post('/playsuccesc', async function (req, res, next) {
    let token = req.body.token
    if (adminloginToken !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let id = req.body._id
    let info = await mg.findOne("playorder", {_id: mg.ObjectID(id)})

    incdianshu(info.userid, info.dianshu, "充值")
    await mg.updateOne("hezuoshang", {_id: mg.ObjectID(info.userid)}, {$inc: {dianshu: info.dianshu}})
    await mg.updateOne("playorder", {_id: info._id}, {$set: {is_relady: 1}})
    let list = await mg.find("playorder", {is_relady: 0})
    pu.successadmin("成功", list, res)

})
router.get('/daozhuang', async function (req, res, next) {

    let data = req.query
    let info = await mg.findOne("playorder", {orderid: Number(data.out_trade_no)})
    if (!info || info.is_relady == 1) {
        res.end()
        return
    } else {
        if (data.trade_status == "TRADE_SUCCESS" && data.out_trade_no == info.orderid.toString()) {
            incdianshu(info.userid, info.dianshu, "充值")
            await mg.updateOne("hezuoshang", {_id: mg.ObjectID(info.userid)}, {$inc: {dianshu: info.dianshu}})
            await mg.updateOne("playorder", {_id: info._id}, {$set: {is_relady: 1}})

        }
        res.end()
    }

})
router.post('/tixian', async function (req, res, next) {
    let _id = req.body.id
    let token = req.body.token
    let dianshu = req.body.dianshu
    let zhifubao = req.body.zhifubao
    if (await redis.get(_id) !== token || _id.length != 24) {
        pu.failadmin("登录过期", res)
        return
    }

    if (!dianshu || isNaN(Number(dianshu))) {
        pu.failadmin("点数格式不对", res)
        return
    }
    if (dianshu.toString().indexOf(".") != -1) {
        pu.failadmin("点数必须是整数", res)
        return
    }

    let userInfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(_id)})
    if (!userInfo) {
        pu.failadmin("非法操作", res)
        return
    }

    let info = {
        lastdianshu: userInfo.dianshu,
        time: new Date().getTime(),
        dianshu: dianshu,
        zhifubao: zhifubao,
        yue: userInfo.dianshu - dianshu,
        email: userInfo.email,
        is_ready: 0,
        userid: userInfo._id.toString()
    }
    await mg.update("hezuoshang", {_id: mg.ObjectID(_id)}, {$inc: {dianshu: -dianshu}})
    incdianshu(_id, -dianshu, "提现")
    await mg.insert("tixian", info)
    pu.sendMail("HY TX新订单通知", ['1437423@qq.com', "511506871@qq.com", "1984843273@qq.com", "921623243@qq.com"], "您有新订单通知" + info.email + ":" + info.dianshu)
    pu.successadmin("成功", userInfo.dianshu - dianshu, res)
})
router.post('/plays', async function (req, res, next) {
    let _id = req.body.id
    let token = req.body.token
    let dianshu = req.body.dianshu
    if (await redis.get(_id) !== token || _id.length != 24) {
        pu.failadmin("登录过期", res)
        return
    }
    if (!dianshu || isNaN(Number(dianshu))) {
        pu.failadmin("点数格式不对", res)
        return
    }
    if (dianshu.toString().indexOf(".") != -1) {
        pu.failadmin("点数必须是整数", res)
        return
    }


    let userInfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(_id)})
    if (!userInfo) {
        pu.failadmin("非法操作", res)
        return
    }
    // let playinfs = await mg.findOne("playorder", {userid: userInfo._id.toString(), is_relady: 0})
    // if (playinfs) {
    //     pu.failadmin("您有未到账订单", res)
    //     return
    // }

    var orderid = new Date().valueOf()

    let url = await play.play(orderid, dianshu, userInfo.email)

    let plinfo = {
        userid: _id,
        orderid: orderid,
        dianshu: Number(dianshu),
        email: userInfo.email,
        time: new Date().getTime(),
        is_relady: 0
    }

    await mg.insert("playorder", plinfo)
    pu.successadmin("成功", url, res)

})

router.post('/findplaysNot', async function (req, res, next) {
    let _id = req.body.id
    let token = req.body.token
    if (await redis.get(_id) !== token || _id.length != 24) {
        pu.failadmin("登录过期", res)
        return
    }
    let notlist = await mg.findOne("playorder", {userid: _id, is_relady: 0})
    pu.successadmin("成功", notlist, res)

})

router.post('/playdelete', async function (req, res, next) {
    let token = req.body.token
    if (adminloginToken !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let id = req.body._id
    await mg.deleteOne("playorder", {_id: mg.ObjectID(id)})
    let list = await mg.find("playorder", {is_relady: 0})
    pu.successadmin("成功", list, res)


})

router.post('/quedingdianshu', async function (req, res, next) {
    let token = req.body.token
    if (adminloginToken !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let id = req.body._id
    await mg.update("tixian", {_id: mg.ObjectID(id)}, {$set: {is_ready: 1}})
    let list = await mg.find("tixian", {is_ready: 0})
    pu.successadmin("成功", list, res)
})
router.post('/deletebudaozhuang', async function (req, res, next) {
    let token = req.body.token
    if (adminloginToken !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let id = req.body._id
    let info = await mg.findOne("tixian", {_id: mg.ObjectID(id)})
    console.log(info)
    await mg.update("hezuoshang", {_id: mg.ObjectID(info.userid)}, {$inc: {dianshu: Number(info.dianshu)}})

    await mg.update("tixian", {_id: mg.ObjectID(id)}, {$set: {is_ready: 1}})
    let list = await mg.find("tixian", {is_ready: 0})
    pu.successadmin("成功", list, res)
})
router.post('/findplayList', async function (req, res, next) {
    let token = req.body.token
    if (adminloginToken !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let list = await mg.find("playorder", {is_relady: 0})
    pu.successadmin("成功", list, res)
})

router.post('/findtixianlis', async function (req, res, next) {

    let token = req.body.token
    if (adminloginToken !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let list = await mg.find("tixian", {is_ready: 0})
    pu.successadmin("成功", list, res)
})


router.post('/updateconfig', async function (req, res, next) {
    let _id = req.body.id
    let token = req.body.token
    let name = req.body.name
    let phones = req.body.phones
    let jiejian = req.body.jiejian

    if (await redis.get(_id) !== token) {
        pu.failadmin("登录过期", res)
        return
    } else {
        await  mg.update("hezuoshang", {_id: mg.ObjectID(_id)}, {$set: {name: name, jiejian: jiejian, phones: phones}})
        pu.successadmin("成功", "", res)

    }
});

router.post('/findKeys', async function (req, res, next) {
    let _id = req.body.id
    let token = req.body.token
    if (await redis.get(_id) !== token) {
        pu.failadmin("登录过期", res)
        return
    } else {
        let list = await mg.find("keys", {uid: mg.ObjectID(_id)})
        pu.successadmin("成功", list, res)
    }
});
router.post('/deleteKeys', async function (req, res, next) {
    let id = req.body.id
    let _id = req.body._id
    let token = req.body.token
    if (await redis.get(id) !== token) {
        pu.failadmin("登录过期", res)
        return
    } else {
        await mg.deleteOne("keys", {_id: mg.ObjectID(_id)})
        pu.successadmin("成功", "", res)
    }
});

router.post('/getTestkey', async function (req, res, next) {
    let id = req.body.id
    let token = req.body.token
    if (await redis.get(id) !== token) {
        pu.failadmin("登录过期", res)
        return
    } else {
        let userinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(id)})
        if (userinfo.testkey >= userinfo.alltestKey) {
            pu.failadmin("获取测试卡次数上限 可以升级获取更多测试卡", res)
            return
        }
        let keys = uuid.v4()
        let infos = {
            uid: mg.ObjectID(id),
            stopTime: 0,
            keys: keys,
            starttime: 1000 * 60 * 60,
            time: new Date().getTime(),
            test: "1"
        }
        await mg.updateOne("hezuoshang", {_id: mg.ObjectID(id)}, {$inc: {testkey: 1}})
        await mg.insert("keys", infos)
        pu.successadmin("成功", {keys: keys}, res)
    }
});


router.post('/getroom', async function (req, res, next) {
    let rdata = {
        room: await mg.find("room", {})
    }
    pu.success(5, rdata, res)
})


router.post('/finddiansulist', async function (req, res, next) {
    let id = req.body.id
    let token = req.body.token
    if (await redis.get(id) !== token) {
        pu.failadmin("登录过期", res)
        return
    }
    let list = await mg.find("dianshu", {uid: id}, {msg: 1, time: 1, dianshu: 1}, 0, 10, {_id: -1})
    pu.successadmin("成功", list, res)
})


router.post('/keys', async function (req, res, next) {
    var key = 0
    var token = req.body.token || uuid.v4()

    if (req.body.keys) {
        let info = await mg.findOne("keys", {keys: req.body.keys})
        if (!info || (info.stopTime > 0 && new Date().getTime() >= info.stopTime)) {
            key = 0
        } else {
            if (info.stopTime == 0) {
                info.stopTime = new Date().getTime() + info.starttime
                mg.updateOne("keys", {_id: info._id}, {$set: {stopTime: info.stopTime}})
            }
            key = info.stopTime
            await redis.set(req.body.keys, token, 1 * 60 * 60 * 24 * 7)
        }
    } else {
        key = 0
    }
    try {
        if (req.body.hid.length == 24) {
            let info = await mg.findOne("hezuoshang", {_id: mg.ObjectID(req.body.hid)})
            if (info) {
                delete info.pwd
                pu.success("", {info: info, key: key, token: token, time: "1122", msg: "顶盘功能已经上线 请联系客户更新"}, res)
            } else {
                pu.success("", {info: null, key: key, token: token, time: "1122", msg: "顶盘功能已经上线 请联系客户更新"}, res)
            }
        } else {
            pu.success("", {info: null, key: key, token: token, time: "1122", msg: "顶盘功能已经上线 请联系客户更新"}, res)
        }
    } catch (e) {
        console.log(e)
        pu.success("", {info: null, key: key, token: null, time: "1122"}, res)
    }

});


router.post('/ping', async function (req, res, next) {
    let tokens = await redis.get(req.body.key)
    if (!tokens) {
        pu.success("", "登录过期", res)
        return
    }
    if (!req.body.token) {
        pu.success("", "登录过期", res)
        return
    }
    if (tokens != req.body.token) {
        pu.success("", "登录过期", res)
        return
    } else {
        pu.success("", "pong", res)
    }
})

router.post('/updateuserinfo', async function (req, res, next) {
    let cookie = req.body.cookie
    let uid = cookie.split("udb_uid=")[1].split("; ")[0]
    mg.update("cookie", {uid: uid}, {$set: {cookie: req.body.cookie, uid: uid, bean: req.body.bean}}, true, false)
    pu.success("", "ok", res)
})
router.post('/findGameroom', async function (req, res, next) {
    let data = req.body.key
    if (!data) {
        pu.failadmin("数据不正确", res)
        return
    }
    try {
        var s = JSON.parse(pu.decryption(data))
        let keyinfo = await mg.findOne("keys", {keys: s.key})
        if (!keyinfo) {
            pu.failadmin("数据不正确", res)
            return
        }
        if (keyinfo.stopTime < new Date().getTime()) {
            pu.failadmin("卡密已过期", res)
            return
        }
        let listInfo = await mg.find("room", {})
        pu.successadmin("ok", {list: listInfo}, res)
    } catch (e) {
        console.log(e)
        pu.failadmin("秘钥不正确", res)
        return
    }


})
router.post('/getData', async function (req, res, next) {

    let data = req.body.key
    let time = req.body.time
    if (!data || !time) {
        pu.failadmin("数据不正确", res)
        return
    }
    try {
        var s = JSON.parse(pu.decryption(data))
        let keyinfo = await mg.findOne("keys", {keys: s.key})
        if (!keyinfo) {
            pu.failadmin("数据不正确", res)
            return
        }
        if (keyinfo.stopTime < new Date().getTime()) {
            pu.failadmin("卡密已过期", res)
            return
        }
        let listInfo = await mg.find("roomgame", {room: s.room, time: time}, {}, "", "", {_id: -1})
        pu.successadmin("ok", {room: s.room, list: listInfo}, res)
    } catch (e) {
        console.log(e)
        pu.failadmin("秘钥不正确", res)
        return
    }

})
module.exports = router;


async function playdis(id, key,meemail) {
    let userinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(id)}) //自己的信息


    if (!userinfo.lastUserid) {
        return
    } else {
        let lastuserinfo = await mg.findOne("hezuoshang", {_id: mg.ObjectID(userinfo.lastUserid)})//上一個

        let xuyaodianshu = userinfo[key] - lastuserinfo[key]

        incdianshu(userinfo._id.toString(), xuyaodianshu,meemail + "代理消费返点")

        await mg.updateOne("hezuoshang", {_id: userinfo._id}, {$inc: {dianshu: xuyaodianshu}})
        playdis(lastuserinfo._id.toString(), key, userinfo.email)
    }

}


//记录点数日志
function incdianshu(uid, dianshu, msg) {
    mg.insert("dianshu", {uid: uid, time: pu.formatDateTime(1), dianshu: dianshu, msg: msg})
}